home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / dev / e / eiffel.lha / flc / framework / MATH < prev    next >
Encoding:
Text File  |  1996-01-22  |  1.9 KB  |  89 lines

  1.  
  2. -- Integer math class.
  3. -- Can't be created, inherit from it!
  4.  
  5. indexing
  6.   names: math;
  7.   author: "Guichard Damien";
  8.   created: 10,November,1995;
  9.   modified: 10,November,1995
  10.  
  11. class MATH creation   -- empty creation clause, so can't be created.
  12. feature {NONE}
  13.   -- Facilities for heirs only.
  14.   abs(n:INTEGER):INTEGER is
  15.     -- Absolute value of n.
  16.     do
  17.       if n >= 0 then Result := n else Result := -n end
  18.     end;  -- abs
  19.   max(a,b:INTEGER):INTEGER is
  20.     -- Maximum value of a and b.
  21.     do
  22.       if a >= b then Result := a else Result := b end
  23.     end;  -- max
  24.   min(a,b:INTEGER):INTEGER is
  25.     -- Minimum value of a and b.
  26.     do
  27.       if a <= b then Result := a else Result := b end
  28.     end;  -- min
  29.   even(n:INTEGER):BOOLEAN is
  30.     -- Is 'n' even?
  31.     do
  32.       Result := n \\ 2 = 0
  33.     ensure -- Result = n \\ 2 = 0
  34.     end;  -- even
  35.   odd(n:INTEGER):BOOLEAN is
  36.     -- Is 'n' odd?
  37.     do
  38.       Result := n \\ 2 = 1
  39.     ensure -- Result = n \\ 2 = 1
  40.     end;  -- odd
  41.   gcd(a,b:INTEGER):INTEGER is
  42.     -- Greater common divider of a and b.
  43.     require -- a >= 0; b >= 0
  44.     local c:INTEGER
  45.     do
  46.       from
  47.         Result := a;
  48.         c := b
  49.       until Result = c
  50.       loop
  51.         if Result > c then
  52.           Result := Result - c
  53.         else
  54.           c := c - Result
  55.         end
  56.       end
  57.     end;  -- pgcd
  58.   power(n,p:INTEGER):INTEGER is
  59.     -- n ^ p.
  60.     require -- p >= 0
  61.     local i:INTEGER
  62.     do
  63.       if p = 0 then
  64.         Result := 0
  65.       else
  66.         from
  67.           i := 1;
  68.           Result := n
  69.         until i = p
  70.         loop
  71.           i := i + 1;
  72.           Result := Result * n
  73.         end
  74.       end
  75.     ensure -- p = 0 implies Result = 0
  76.     end;  -- power
  77.   faculty(n:INTEGER):INTEGER is
  78.     -- n!
  79.     require -- n >= 0
  80.     do
  81.       if n = 0 then
  82.         Result := 1
  83.       else
  84.         Result := n * faculty(n-1)
  85.       end
  86.     end  -- faculty
  87. end  -- class 'MATH'
  88.  
  89.